# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By

# 实例化，以Chrome打开
# driver = webdriver.Chrome()

# chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument("--headless")
# chrome_options.add_argument("--proxy-server=http://127.0.0.1:9743")

# driver = webdriver.Chrome(chrome_options=chrome_options)

# driver.get("https://www.baidu.com")
# driver.get("https://passport.bilibili.com/login")

# 调整浏览器窗口大小
# driver.maximize_window()  # 最大化
# driver.set_window_size(1920, 1080)

# 保存截屏
# driver.save_screenshot("jietu.png")

# 在input标签中输入内容
# driver.find_element_by_id("kw").send_keys("python")
# driver.find_element_by_id("su").click()  # 点击事件

# 获取当前页面源码
# print(driver.page_source)

# 获取当前url地址
# print(driver.current_url)

# 获取cookies
# print(driver.get_cookies())
# print(driver.get_network_conditions())

# 程序结束退出浏览器
# driver.quit()
# driver.close()

# 获取标签属性
# tag = driver.find_element_by_class_name('geetest_panel_success')
# res = tag.value_of_css_property('display')
# print(res)


# tag = driver.find_element_by_class_name('geetest_panel_success')
# print(tag.is_displayed())


def init_selelium(ua):
    from selenium import webdriver
    # Headless模式，无界面
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("user-agent='{}'".format(ua))
    driver = webdriver.Chrome(options=options)

    # 响应式缩小页面，改变页面布局
    driver.set_window_size(500, 1080)
    # get_element(driver, url, attr)
    # driver.get(url)
    # element = driver.find_element_by_class_name(attr)
    # import time
    # time.sleep(2)
    # element.click()
    return driver


def get_element(driver, url, attr):
    driver.get(url)
    # element = driver.find_element_by_class_name(attr)
    elements = driver.find_elements_by_xpath(attr)
    # if element:
    #     import time
    #     time.sleep(2)
    #     element.click()
    return elements


def close_driver(driver):
    return driver.close()

ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"

driver = init_selelium(ua)
elements = get_element(driver,"https://m.dianping.com/citylist?c=A", "//ul[@class='J_citylist']/li/a")
for ele in elements:
    attr = ele.get_attribute("href")
    print(attr)
close_driver(driver)